import 'package:environmental_les_app/common/routes/route_names.dart';
import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/utils/utils.dart';
import 'package:environmental_les_app/common/widgets/button_public_rect.dart';
import 'package:environmental_les_app/common/widgets/input_dialog.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';

import 'index.dart';

class MinePage extends GetView<MineController> {
  const MinePage({super.key});

  // 主视图
  Widget _buildView() {
    return Column(
      children: [
        vGap40,
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            hGap15,
            ImageWidget.loadAssetImage("ic_mine_avatar", width: 70, height: 70),
            hGap10,
            Expanded(
              child: Column(
                children: [
                  Row(
                    children: [
                      Text(
                        controller.userInfo["nickname"] ?? "",
                        style: const TextStyle(
                            color: Colours.text171A1D,
                            fontSize: 18,
                            fontWeight: FontWeight.w500),
                      ),
                      hGap8,
                      Container(
                        padding: const EdgeInsets.symmetric(
                            horizontal: 4, vertical: 2),
                        decoration: BoxDecoration(
                          color: Colours.bgCBDCF8,
                          borderRadius: BorderRadius.circular(4),
                        ),
                        height: 22,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            ImageWidget.loadAssetImage("ic_mine_position",
                                width: 16, height: 16),
                            hGap2,
                            Text(
                              controller.userInfo["deptName"] ?? "",
                              style: const TextStyle(
                                  color: Colours.textC4C,
                                  fontSize: 12,
                                  fontWeight: FontWeight.w500),
                            )
                          ],
                        ),
                      )
                    ],
                  ),
                  vGap5,
                  Row(
                    children: [
                      ImageWidget.loadAssetImage("ic_mine_company",
                          width: 16, height: 16),
                      hGap2,
                      const Text(
                        "准格尔旗环境监察大队",
                        style: TextStyle(
                            color: Colours.text969,
                            fontSize: 14,
                            fontWeight: FontWeight.w400),
                      )
                    ],
                  )
                ],
              ),
            ),
          ],
        ),
        vGap20,
        Container(
          margin: const EdgeInsets.symmetric(horizontal: 15),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(6.0),
          ),
          child: Column(
            children: [
              _buildFunctionItem(
                title: "联系电话",
                icon: "ic_mine_phone",
                rightTitle: controller.userInfo["mobile"] ?? "",
                onTap: () {
                  Get.dialog(InputDialog(
                    title: "修改联系电话",
                    hintText: "请输入联系电话",
                    onConfirm: (value) {
                      // 处理确认逻辑
                      logger.d("输入的联系电话: $value");
                    },
                  ));
                },
              ),
              _buildFunctionItem(
                title: "邮箱",
                rightTitle: controller.userInfo["email"] ?? "",
                icon: "ic_mine_email",
                onTap: () {
                  Get.dialog(InputDialog(
                    title: "修改邮箱",
                    hintText: "请输入邮箱",
                    onConfirm: (value) {
                      // 处理确认逻辑
                      logger.d("输入的邮箱: $value");
                      if (emailRegExp.hasMatch(value)) {
                        // 进行邮箱格式校验
                        logger.d("邮箱格式正确");
                      } else {
                        // 提示邮箱格式不正确
                        logger.d("邮箱格式不正确");
                      }
                    },
                  ));
                },
              ),
              _buildFunctionItem(
                title: "通讯录",
                icon: "ic_mine_book",
                onTap: () {
                  Get.toNamed(AppRoutes.addressBookPage);
                },
              ),
              _buildFunctionItem(
                title: "知识库",
                icon: "ic_mine_library",
                onTap: () {
                  Get.toNamed(AppRoutes.knowledgeLibraryPage);
                },
              ),
              _buildFunctionItem(
                  title: "当前版本",
                  rightTitle: "v1.0.0",
                  icon: "ic_mine_version",
                  onTap: () {},
                  showLine: false)
            ],
          ),
        ),
        Container(
          padding: const EdgeInsets.symmetric(vertical: 30, horizontal: 15),
          child: ButtonPublicRect(
              text: "退出登录",
              type: ButtonPublicType.outline,
              minWidth: double.infinity,
              onPressed: () {
                controller.logout();
              }),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MineController>(
      init: MineController(),
      id: "mine",
      builder: (_) {
        return Scaffold(
          body: Stack(
            children: [
              Container(
                height: 255,
                decoration: BoxDecoration(
                    image: DecorationImage(
                  image: AssetImage(ImageUtils.getImgPath("bg_mine_top")),
                  fit: BoxFit.cover,
                )),
              ),
              SafeArea(
                child: EasyRefresh.builder(
                  onRefresh: controller.onRefresh,
                  header: const TaurusHeader(skyColor: Colours.bgE6F2FE),
                  childBuilder: (context, physics) {
                    return SingleChildScrollView(
                      physics: physics,
                      child: _buildView(),
                    );
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  // 个人中心 功能点击 item
  Widget _buildFunctionItem(
      {required String title,
      required String icon,
      required VoidCallback onTap,
      bool showLine = true,
      bool showArrow = true,
      String? rightTitle}) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 15),
        margin: const EdgeInsets.symmetric(horizontal: 15),
        decoration: BoxDecoration(
          border: showLine
              ? const Border(
                  bottom: BorderSide(
                    color: Colours.lineE5E6EB,
                    width: 1,
                  ),
                )
              : null,
        ),
        child: Row(
          children: [
            ImageWidget.loadAssetImage(icon, width: 30, height: 30),
            hGap8,
            Expanded(
              child: Text(
                title,
                style: const TextStyle(
                  color: Colours.text09C,
                  fontSize: 15,
                  fontWeight: FontWeight.normal,
                ),
              ),
            ),
            Text(
              rightTitle ?? "",
              style: const TextStyle(
                color: Colours.text254,
                fontSize: 15,
                fontWeight: FontWeight.w500,
              ),
            ),
            hGap10,
            if (showArrow)
              const Icon(
                Icons.arrow_forward_ios,
                size: 15,
                color: Colours.iconColor,
              ),
          ],
        ),
      ),
    );
  }
}
